const Koa = require('koa');
const app = new Koa();

const indent = (n) => new Array(n).join('&nbsp;')

app.use(async (ctx, next) => {
  ctx.body = `<h1>请求：第一层中间件</h1>`
  await next()
  ctx.body += `<h1>响应：第一层中间件${JSON.stringify(ctx.state.user)}</h1>`
})

app.use(async (ctx, next) => {
  ctx.body += `<h1>${indent(4)}请求：第二层中间件</h1>`
  await next()
  ctx.body += `<h1>${indent(4)}响应：第二层中间件</h1>`
})

app.use(async (ctx, next) => {
  ctx.body += `<h1>${indent(8)}请求：第三层中间件</h1>`
  await next()
  ctx.body += `<h1>${indent(8)}响应：第三层中间件</h1>`
})

app.use(async (ctx, next) => {
  ctx.state.user = { username: 'devin', pwd: '123' }
  ctx.body += `<h4 >${indent(18)}核心业务</h4>`
})


app.listen(8000, () => {
  console.log('server port is 8000')
})